using SharpDX;
using System;
using System.Collections.Generic;
using System.IO;

namespace Engine.Graphics
{
    public class Material
    {
        #region Private Fields

        private static int randomColor = 1;
        private string _materialName;
        private Color4 color;
        private uint hashCode;

        /// <summary>
        /// Meshes and SubsetId
        /// </summary>
        private Dictionary<Meshobject, List<int>> registeredMeshes;

        #endregion Private Fields

        #region Public Constructors

        public Material()
        {
            this.registeredMeshes = new Dictionary<Meshobject, List<int>>();
            // this.color = new Color4(1, 1, 1, 1);
            this.color = new Color4(GetRandomColor());
            this._materialName = string.Empty;
        }

        public Material(IMaterial gameMaterial)
            : this()
        {
            GameMaterialProperties = gameMaterial;
        }

        #endregion Public Constructors

        #region Public Properties

        public IMaterial GameMaterialProperties { get; set; }

        public Color4 MaterialColor
        {
            get { return color; }
            set { color = value; }
        }

        public string MaterialName
        {
            get
            {
                if (this._materialName != string.Empty)
                    return _materialName;
                else
                    return this.GameMaterialProperties.ToString();
            }

            set { _materialName = value; }
        }

        [System.Xml.Serialization.XmlIgnoreAttribute]
        public Dictionary<Meshobject, List<int>> RegisteredMeshes
        {
            get { return registeredMeshes; }
        }

        #endregion Public Properties

        #region Public Methods

        public override string ToString()
        {
            return this.MaterialName.ToString();
        }

        public static Material FromStream(BinaryReader br, AddinManager.IAddin addin, MaterialManager materialManager)
        {
            Material material = new Material(addin.LoadMaterial(br, materialManager));

            return material;
        }

        public static bool operator !=(Material material1, Material material2)
        {
            if (material1.hashCode != material2.hashCode) return true; return false;
        }

        public static bool operator ==(Material material1, Material material2)
        {
            if (material1.hashCode == material2.hashCode) return true; return false;
        }

        public override int GetHashCode()
        {
            return (int)this.GameMaterialProperties.GetHashCode();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="mesh">the mesh which uses this Material</param>
        /// <param name="subsetId">the subset which uses this Material</param>
        public void RegisterMesh(Meshobject meshId, int subsetId)
        {
            List<int> subsets;
            lock (this.registeredMeshes)
                if (this.registeredMeshes.ContainsKey(meshId))
                {
                    var item = this.registeredMeshes.TryGetValue(meshId, out subsets);
                    subsets.Add(subsetId);
                }
                else
                {
                    subsets = new List<int>();
                    subsets.Add(subsetId);
                    lock (this.registeredMeshes)
                        this.registeredMeshes.Add(meshId, subsets);
                }
        }

        public void SaveToStream(BinaryWriter bw)
        {
            GameMaterialProperties.SaveToStream(bw);
        }

        #endregion Public Methods

        #region Internal Methods

        internal void UnregisterMesh(Meshobject mesh)
        {
            lock (this.registeredMeshes)
                if (this.registeredMeshes.ContainsKey(mesh))
                    this.registeredMeshes.Remove(mesh);
        }

        #endregion Internal Methods

        #region Private Methods

        /// <summary>
        /// Create a Random color by random ints
        /// </summary>
        /// <returns></returns>
        private Color4 GetRandomColor()
        {
            Random random = new Random(Material.randomColor);
            Material.randomColor = random.Next();
            return new Color4((float)random.Next(256) / 255, (float)random.Next(256) / 255, (float)random.Next(256) / 255, 1.0f);
        }

        #endregion Private Methods
    }
}